package com.corner.presenter.imp;

import android.content.Context;
import android.content.Intent;

import com.corner.R;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.helper.UserHelper;
import com.corner.model.leancloud.helper.MessageHelperV2;
import com.corner.presenter.IUserSettingItemPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.activity.ChangePasswordActivity;
import com.corner.ui.activity.LoginAndRegisterActivity;
import com.corner.ui.view.IUserSettingItemView;
import com.corner.utils.AppManager;
import com.corner.utils.NetUtil;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/16，00.09.
 * 描述：
 */
public class UserSettingItemPresenter extends BasePresenter<IUserSettingItemView> implements IUserSettingItemPresenter {
    @Override
    public void changeUsername(String username) {
        view.showLoading(true);
        UserHelper.changeUsername(User.getCurrentUser(), username).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<User>() {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.changeUsernameError(e.getMessage());
                    view.showLoading(false);
                }
            }

            @Override
            public void onNext(User user) {
                if (view != null) {
                    view.changeUsernameSucess();
                    view.showLoading(false);
                }
            }
        });
    }


    @Override
    public void changePassword(final Context context) {
        view.showLoading(true);
        UserHelper.getUser(User.getCurrentUser().getObjectId()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<User>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.changePasswordError(e.getMessage());
                    view.showLoading(false);
                }
            }

            @Override
            public void onNext(User user) {
                if (view != null) {
                    if (user != null) {
                        if (user.getHasSetPassword() != null && user.getHasSetPassword()) {
                            Intent intent = new Intent(context, ChangePasswordActivity.class);
                            context.startActivity(intent);
                        } else {
                            view.showChangePasswordHasNoPasswordDialog();
                        }
                    }
                    view.showLoading(false);
                }
            }
        });

    }


    @Override
    public void getIsHasPasswordBeforeLogout(final Context context) {
        if (NetUtil.checkNet(App.application)) {
            view.showLoading(true);
            UserHelper.getUser(User.getCurrentUser().getObjectId()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<User>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (view != null) {
                                view.logoutError(e.getMessage());
                                view.showLoading(false);
                            }
                        }

                        @Override
                        public void onNext(User user) {
                            if (view != null) {
                                if (user.getHasSetPassword() != null && user.getHasSetPassword()) {
                                    view.showLogoutEnsureDialog();
                                } else {
                                    view.showLogoutHasNoPasswordDialog();
                                }
                                view.showLoading(false);
                            }
                        }
                    });
        } else {
            view.showError(context.getString(R.string.request_net_error));
        }
    }

    @Override
    public void logout(Context context) {
        AppManager.getInstance().finishAllActivity();
        MessageHelperV2.closeImClient();
        User.logOut();
        App.application.clearDaos();
        Intent intent = new Intent(context, LoginAndRegisterActivity.class);
        context.startActivity(intent);
    }


}
